package sandbox.vaadin.skiller.skillgroupmanager;

import org.apache.log4j.Logger;

import sandbox.vaadin.Util;
import sandbox.vaadin.skiller.entity.Skillgroup;
import sandbox.vaadin.skiller.entity.SkillgroupEditor;

import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Tree;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class SkillgroupTreeView extends VerticalLayout
{
  private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(SkillgroupTreeView.class);

	private SkillgroupManagerContext context;

	public SkillgroupTreeView(SkillgroupManagerContext context)
	{
		super();
		this.context = context;
		init();
	}

	public SkillgroupTreeView(SkillgroupManagerContext context, Util.Size size)
	{
		this(context);
		Util.setSize(this, size);		
	}

	public SkillgroupTreeView(
			SkillgroupManagerContext context, Util.Size width, Util.Size height)
  {
		this(context);		
		Util.setSize(this, width, height);
  }

	private void init()
	{
		addComponent(newSkillgroupsTree());
		addComponent(newSkillgroupsButtonBar());
	}

	private Component newSkillgroupsButtonBar()
  {
		HorizontalLayout result = new HorizontalLayout();

		result.addComponent(newButtonEditSkillgroup());

		return result;
  }

	private Component newSkillgroupsTree()
  {
		Tree result = new Tree("skill groups", context.getSkillgroupContainer());

		result.setItemCaptionMode(ItemCaptionMode.PROPERTY);
		result.setItemCaptionPropertyId("name");
		result.setImmediate(true);
		result.addValueChangeListener(new SkillgroupValueChangeListener());

		return result;
  }

	private class SkillgroupValueChangeListener implements ValueChangeListener
	{
		private static final long serialVersionUID = 1L;

		@Override
		public void valueChange(ValueChangeEvent event)
		{
			Object value = event.getProperty().getValue();

			LOGGER.debug("event.property.value: " + value);
			LOGGER.debug("event.property.type: " + event.getProperty().getType());

			if (value != null)
			{
				context.setSkillgroupSelectedEntityItem(
						context.getSkillgroupContainer().getItem(value));
//				LOGGER.debug("entity item: " + context.getSkillgroupSelectedEntityItem());
//				LOGGER.debug("entity: " + getSkillgroupSelected());
			}
			else
			{
				// happens if user clicks on already selected node
			}
		}
	}
	
	private Skillgroup getSkillgroupSelected()
	{
		EntityItem<Skillgroup> skillgroupSelectedEntityItem =
				context.getSkillgroupSelectedEntityItem();

		if (skillgroupSelectedEntityItem != null)
		{
			return skillgroupSelectedEntityItem.getEntity();
		}

		return null;
	}

	private Component newButtonEditSkillgroup()
	{
		Button result = new Button("edit");

		result.addClickListener(new ButtonEditSkillgroupListener());
		
		return result ;
	}

	private class ButtonEditSkillgroupListener implements Button.ClickListener
	{
		private static final long serialVersionUID = 1L;

		@Override
    public void buttonClick(ClickEvent event)
    {
			LOGGER.debug("edit button clicked");
			
			Skillgroup skillgroupSelected = getSkillgroupSelected();
			
			if (skillgroupSelected != null)
			{
				LOGGER.debug("selected skill group: " + skillgroupSelected);
				UI.getCurrent().addWindow(
						new SkillgroupEditor(context.getSkillgroupSelectedEntityItem())); 
			}
    }
	}
}